/**
* Abstract: String Localization Manager
*
* Version: 1.0
*
* Disclaimer: IMPORTANT:  This Gamenova software is supplied to you by
* 4E Studios Srl("4E") in consideration of your agreement to the following terms,
* and your use, installation, modification or redistribution of this 4E software
* constitutes acceptance of these terms.  If you do not agree with these terms,
* please do not use, install, modify or redistribute this 4E software.
*
* In consideration of your agreement to abide by the following terms, and subject
* to these terms, 4E grants you a personal, non-exclusive license, under
* 4E's copyrights in this original 4E software (the "4E Software"), to
* use, reproduce, modify and redistribute the 4E Software, with or without
* modifications, in source and/or binary forms; provided that if you redistribute
* the 4E Software in its entirety and without modifications, you must retain
* this notice and the following text and disclaimers in all such redistributions
* of the 4E Software.
* Neither the name, trademarks, service marks or logos of 4E Studios Srl may be used
* to endorse or promote products derived from the 4E Software without specific
* prior written permission from 4E.  Except as expressly stated in this notice,
* no other rights or licenses, express or implied, are granted by 4E herein,
* including but not limited to any patent rights that may be infringed by your
* derivative works or by other works in which the 4E Software may be
* incorporated.
*
* The 4E Software is provided by 4E on an "AS IS" basis.  4E MAKES NO
* WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, REGARDING THE 4E SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
* COMBINATION WITH YOUR PRODUCTS.
*
* IN NO EVENT SHALL 4E BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
* DISTRIBUTION OF THE 4E SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
* CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
* 4E HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright (C) 2011 4E Studios Srl All Rights Reserved.
*/

/**
	String localization manager class
	@constructor
*/
function GNLocale()
{
	//init actual language by navigator language or by english (default)
	this.actualLanguage = (navigator.language) ? navigator.language : navigator.userLanguage; 
	if(!this.actualLanguage) this.actualLanguage = "en";

	//init an empty conversion table
	this.localeConversionTable = {};
	this.localeConversionTable[this.actualLanguage] = {};

	/**
		Add / load new languages data. 
		data could be json object or a parsable json string in this format:
		
		{
			langId : 
			{
				itemKeyStr : itemValStr
			}
		}
	
		where langId is a standard lang code (ex. 'en', 'it') 
		itemKeyStr the string key to convert and itemValStr his (eventually escaped) value.
	*/
	this.addLocaleData = function(jsonConfigData)
	{
		var data = jsonConfigData;
		if(typeof jsonConfigData == "string") data = JSON.parse(jsonConfigData);

		for(var langIds in data)
		{
			if(!this.localeConversionTable[langIds]) this.localeConversionTable[langIds] = {};
			var langData = data[langIds];

			for(var itemStr in langData)
			{
				// only for debug
				if(this.localeConversionTable[langIds][itemStr]) warn("GNLocale.addLocaleData : code '" + itemStr + "' localized '" + langIds + "' could be overwritten");
				
				this.localeConversionTable[langIds][itemStr] = langData[itemStr];
			}
		}
	}

	/**
		Change locale language
	*/
	this.changeLanguage = function(langId)
	{
		this.actualLanguage = langId;
		if(!this.localeConversionTable[this.actualLanguage]) this.localeConversionTable[this.actualLanguage] = {};
	}
}

//init
window.GNLocale = new GNLocale();

/**
	Get the string translation for the current locale
*/
String.prototype.toLocaleString = function()
{
	var translatedStr = GNLocale.localeConversionTable[GNLocale.actualLanguage][this];
	if(translatedStr) return unescape(translatedStr);
	else return this.toString();		
};
